package com.sls.web.manager.action.game;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.sls.web.manager.action.SuperAction;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.common.DataUtil;
import com.sls.web.manager.common.FilterStringSpaces;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.game.pojo.GoodsSupply;
import com.sls.web.manager.game.querybean.GoodsSupplyQuery;
import com.sls.web.manager.game.service.GoodsSupplyService;
import com.sls.web.manager.game.service.LocalGoodsNotice;
import com.sls.web.manager.page.Pagination;

@Results(value = { @Result(name = "filedownload", type = "stream", params = {
		"contentType", "application/x-download", "inputName", "istream",
		"contentDisposition", "attachment;filename=${fileName}" }) })
public class GoodsSupplyAction extends SuperAction {

	private static final long serialVersionUID = -4709076795701574177L;
	@Resource(name = "goodsSupplyService")
	private GoodsSupplyService goodsSupplyService;
	private GoodsSupply goodsSupply;
	private String id;
	private String suppliersId;
	private String goodsId;
	private String oriGoodsId;
	private String goodsCategoryId;
	private String supplyName;
	private String originalGoodsId;
	private String goodsCostPrice;
	private String goodsValue;
	private String goodsState;
	private GoodsSupplyQuery goodsSupplyQuery;
	private Pagination<GoodsSupply> page;
	private List<String> supplyNames;
	private String supplierName;
	private String fileName;// 文件名称
	private InputStream istream; // 输入流
	private String goodsName;
	@Resource(name="localGoodsNotice")
	private LocalGoodsNotice localGoodsNotice;

	
	/**
	 * 商品变更，主动通知下游
	 * 
	 * @return
	 * @throws Exception
	 */
	public String noticeUser() throws Exception {
		localGoodsNotice.noticeGoodsModify(goodsId,null,getCurrentOperator());
		try {
			if (goodsSupplyQuery == null) {
				goodsSupplyQuery = new GoodsSupplyQuery();
				goodsSupplyQuery.setPageSize(Constants.INIT_PAGESIZE);
				goodsSupplyQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
			}
			goodsSupplyQuery.setGoodsState("1");
			supplyNames = this.goodsSupplyService
					.querySupplyNames(getCurrentOperator());
			page = goodsSupplyService.queryGoodsSupply(goodsSupplyQuery,
					getCurrentOperator());
		} catch (ManagerException e) {
			addActionError(e.getMessage());
		}
		return SUCCESS;
	}
	
	

	/**
	 * 新增
	 * 
	 * @return
	 */
	public String add() {
		return INPUT;
	}

	/**
	 * 保存
	 * 
	 * @return
	 * @throws Exception
	 */
	public String save() throws Exception {
		try {
			boolean result = goodsSupplyService.add(goodsSupply,
					getCurrentOperator());
			if (!result) {
				return ERROR;
			}
		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
		}
		redirectionUrl = "goods_supply!list.htm";
		return SUCCESS;
	}

	/**
	 * 编辑
	 * 
	 * @return
	 * @throws Exception
	 */
	public String toEdit() throws Exception {
		goodsSupply = goodsSupplyService.queryGoodsSupplyById(id,
				getCurrentOperator());
		if (goodsSupply == null) {
			return ERROR;
		}
		return INPUT;
	}

	/**
	 * 编辑
	 * 
	 * @return
	 * @throws Exception
	 */
	public String edit() throws Exception {

		try {
			int costPrice = DataUtil.toInt(goodsCostPrice);
			int parvalue = DataUtil.toInt(goodsValue);
			goodsSupply.setGoodsCostPrice(costPrice);
			goodsSupply.setGoodsValue(parvalue);
			goodsSupply.setGoodsId(goodsId);
			goodsSupply.setGoodsCategoryId(goodsCategoryId);
			goodsSupply.setGoodsName(goodsName);

			boolean result = goodsSupplyService.update(goodsSupply,
					getCurrentOperator());
			if (!result) {
				return ERROR;
			} else {
				if (goodsSupplyQuery == null) {
					goodsSupplyQuery = new GoodsSupplyQuery();
					goodsSupplyQuery.setPageSize(Constants.INIT_PAGESIZE);
					goodsSupplyQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
				}
				goodsSupplyQuery.setGoodsState("1");
				if (StringUtils.isNotEmpty(goodsId)) {
					goodsSupplyQuery.setGoodsId(goodsId);
				}
				if (StringUtils.isNotEmpty(goodsName)) {
					goodsSupplyQuery.setGoodsName(goodsName);
				}
				if (StringUtils.isNotEmpty(goodsCategoryId)) {
					goodsSupplyQuery.setGoodsCategoryId(goodsCategoryId);
				}
				if (StringUtils.isNotEmpty(supplyName)) {
					goodsSupplyQuery.setSupplyName(supplyName);
				}
				if (StringUtils.isNotEmpty(oriGoodsId)) {
					goodsSupplyQuery.setOriginalGoodsId(oriGoodsId);
				}
			}
		} catch (ManagerException e) {
			addActionError(e.getMessage());
		}
		return list();
	}

	/**
	 * 详情
	 * 
	 * @return
	 * @throws Exception
	 */
	public String detail() throws Exception {
		try {
			goodsSupply = goodsSupplyService.queryGoodsSupplyById(id,
					getCurrentOperator());
		} catch (ManagerException e) {
			return ERROR;
		}
		return "detail";
	}

	/**
	 * 商品上下架修改
	 * 
	 * @return
	 * @throws Exception
	 */
	public String updateState() throws Exception {
		try {
			goodsSupplyService.updateState(id, goodsState, goodsId,
					getCurrentOperator());
		} catch (ManagerException e) {
			addActionError(e.getMessage());
			return ERROR;
		}
		return list();
	}

	/**
	 * 列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String list() throws Exception {
		try {
			if (goodsSupplyQuery == null) {
				goodsSupplyQuery = new GoodsSupplyQuery();
				goodsSupplyQuery.setPageSize(Constants.INIT_PAGESIZE);
				goodsSupplyQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
			}
			goodsSupplyQuery.setGoodsState("1");
			supplyNames = this.goodsSupplyService
					.querySupplyNames(getCurrentOperator());
			page = goodsSupplyService.queryGoodsSupply(goodsSupplyQuery,
					getCurrentOperator());
		} catch (ManagerException e) {
			addActionError(e.getMessage());
		}
		return LIST;
	}

	/**
	 * 搜索后的列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String supplyList() throws Exception {
		try {
			if (goodsSupplyQuery == null) {
				goodsSupplyQuery = new GoodsSupplyQuery();
				goodsSupplyQuery.setPageSize(Constants.INIT_PAGESIZE);
				goodsSupplyQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
			}
			String goodsId = goodsSupplyQuery.getGoodsId();
			String goodsCategoryId = goodsSupplyQuery.getGoodsCategoryId();
			String supplyName = goodsSupplyQuery.getSupplyName();
			String originalGoodsId = goodsSupplyQuery.getOriginalGoodsId();
			goodsId = FilterStringSpaces.checkStringHasSpace(goodsId);
			goodsCategoryId = FilterStringSpaces
					.checkStringHasSpace(goodsCategoryId);
			supplyName = FilterStringSpaces.checkStringHasSpace(supplyName);
			originalGoodsId = FilterStringSpaces
					.checkStringHasSpace(originalGoodsId);
			if (StringUtils.isNotEmpty(goodsId)) {
				goodsSupplyQuery.setGoodsId(goodsId);
			}
			if (StringUtils.isNotEmpty(goodsCategoryId)) {
				goodsSupplyQuery.setGoodsCategoryId(goodsCategoryId);
			}
			if (StringUtils.isNotEmpty(supplyName)) {
				goodsSupplyQuery.setSupplyName(supplyName);
			}
			if (StringUtils.isNotEmpty(originalGoodsId)) {
				goodsSupplyQuery.setOriginalGoodsId(originalGoodsId);
			}
			supplyNames = this.goodsSupplyService
					.querySupplyNames(getCurrentOperator());
			page = goodsSupplyService.queryGoodsSupply(goodsSupplyQuery,
					getCurrentOperator());
		} catch (ManagerException e) {
			addActionError(e.getMessage());
		}
		return LIST;
	}

	/**
	 * 导出excel文件
	 * 
	 * @return
	 * @throws Exception
	 */
	public String export() throws Exception {
		List<GoodsSupply> list = null;
		try {
			list = this.goodsSupplyService.querySupplyInfo(supplierName, "1",
					getCurrentOperator());
		} catch (ManagerException e1) {
			addActionError(e1.getMessage());
			flag = false;
			return SUCCESS;
		}
		String filename = supplierName + ".xls";
		try {
			this.createExcel(filename, list);
		} catch (IOException e) {
			addActionError("导出文件异常!");
			flag = false;
			return SUCCESS;
		}
		return "filedownload";
	}

	/**
	 * 创建excel表格
	 * 
	 * @param path
	 * @param list
	 * @throws IOException
	 */
	public void createExcel(String filenameWeb, List<GoodsSupply> list)
			throws IOException {

		// 第一步，创建一个webbook，对应一个Excel文件
		HSSFWorkbook wb = new HSSFWorkbook();
		// 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
		HSSFSheet sheet = wb.createSheet("供应商价格信息");
		// 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
		HSSFRow row = sheet.createRow(0);
		// 第四步，创建单元格，并设置值表头 设置表头居中
		HSSFCellStyle style = wb.createCellStyle();
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
		HSSFCellStyle style2 = wb.createCellStyle();
		style2.setAlignment(HSSFCellStyle.ALIGN_RIGHT);// 创建一个居右格式
		HSSFCell cell = row.createCell(0);
		cell.setCellValue("供应商名称");
		cell.setCellStyle(style);
		cell = row.createCell(1);
		cell.setCellValue("商品编号");
		cell.setCellStyle(style);
		cell = row.createCell(2);
		cell.setCellValue("商品名称");
		cell.setCellStyle(style);
		cell = row.createCell(3);
		cell.setCellValue("商品原始编号");
		cell.setCellStyle(style);
		cell = row.createCell(4);
		cell.setCellValue("商品面值(元)");
		cell.setCellStyle(style);
		cell = row.createCell(5);
		cell.setCellValue("商品进价(元)");
		cell.setCellStyle(style);

		// 第五步，写入实体数据 实际应用中这些数据从数据库得到，
		if (list != null && !list.isEmpty()) {
			for (int i = 0; i < list.size(); i++) {
				row = sheet.createRow((int) i + 1);
				GoodsSupply supply = (GoodsSupply) list.get(i);
				// 第四步，创建单元格，并设置值
				cell = row.createCell(0);
				cell.setCellStyle(style);
				cell.setCellValue(supply.getSupplyName());
				cell = row.createCell(1);
				cell.setCellStyle(style);
				cell.setCellValue(supply.getGoodsId());
				cell = row.createCell(2);
				cell.setCellStyle(style);
				cell.setCellValue(supply.getGoodsName());
				cell = row.createCell(3);
				cell.setCellStyle(style);
				cell.setCellValue(supply.getOriginalGoodsId());
				cell = row.createCell(4);
				cell.setCellStyle(style);
				double goodsValue = supply.getGoodsValue() * 1.0 / 1000;
				cell.setCellValue(goodsValue);
				cell = row.createCell(5);
				cell.setCellStyle(style);
				double costPrice = supply.getGoodsCostPrice() * 1.0 / 1000;
				cell.setCellValue(costPrice);
			}

		}

		// 第六步，将文件存到指定位置
		fileName = new String(filenameWeb.getBytes("GBK"), "iso8859-1");
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		wb.write(baos);
		istream = new ByteArrayInputStream(baos.toByteArray());
		baos.close();
	}

	public GoodsSupplyService getGoodsSupplyService() {
		return goodsSupplyService;
	}

	public void setGoodsSupplyService(GoodsSupplyService goodsSupplyService) {
		this.goodsSupplyService = goodsSupplyService;
	}

	public GoodsSupply getGoodsSupply() {
		return goodsSupply;
	}

	public void setGoodsSupply(GoodsSupply goodsSupply) {
		this.goodsSupply = goodsSupply;
	}

	public GoodsSupplyQuery getGoodsSupplyQuery() {
		return goodsSupplyQuery;
	}

	public void setGoodsSupplyQuery(GoodsSupplyQuery goodsSupplyQuery) {
		this.goodsSupplyQuery = goodsSupplyQuery;
	}

	public Pagination<GoodsSupply> getPage() {
		return page;
	}

	public void setPage(Pagination<GoodsSupply> page) {
		this.page = page;
	}

	public boolean isFlag() {
		return flag;
	}

	public void setGoodsId(String goodsId) {
		this.goodsId = goodsId;
	}

	public String getGoodsId() {
		return goodsId;
	}

	public String getSuppliersId() {
		return suppliersId;
	}

	public void setSuppliersId(String suppliersId) {
		this.suppliersId = suppliersId;
	}

	public String getOriginalGoodsId() {
		return originalGoodsId;
	}

	public void setOriginalGoodsId(String originalGoodsId) {
		this.originalGoodsId = originalGoodsId;
	}

	public String getGoodsState() {
		return goodsState;
	}

	public void setGoodsState(String goodsState) {
		this.goodsState = goodsState;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getOriGoodsId() {
		return oriGoodsId;
	}

	public void setOriGoodsId(String oriGoodsId) {
		this.oriGoodsId = oriGoodsId;
	}

	public String getGoodsCategoryId() {
		return goodsCategoryId;
	}

	public void setGoodsCategoryId(String goodsCategoryId) {
		this.goodsCategoryId = goodsCategoryId;
	}

	public String getSupplyName() {
		return supplyName;
	}

	public void setSupplyName(String supplyName) {
		this.supplyName = supplyName;
	}

	public String getGoodsCostPrice() {
		return goodsCostPrice;
	}

	public void setGoodsCostPrice(String goodsCostPrice) {
		this.goodsCostPrice = goodsCostPrice;
	}

	public List<String> getSupplyNames() {
		return supplyNames;
	}

	public void setSupplyNames(List<String> supplyNames) {
		this.supplyNames = supplyNames;
	}

	public String getSupplierName() {
		return supplierName;
	}

	public void setSupplierName(String supplierName) {
		this.supplierName = supplierName;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public InputStream getIstream() {
		return istream;
	}

	public void setIstream(InputStream istream) {
		this.istream = istream;
	}

	public String getGoodsName() {
		return goodsName;
	}

	public void setGoodsName(String goodsName) {
		this.goodsName = goodsName;
	}

	public String getGoodsValue() {
		return goodsValue;
	}

	public void setGoodsValue(String goodsValue) {
		this.goodsValue = goodsValue;
	}

}
